package com.cqy.auth.config;

import com.cqy.auth.constant.FromLoginConstant;
import com.cqy.auth.properties.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    SecurityProperties securityProperties;

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }


    @Override
    public void configure(WebSecurity web) {
        web.ignoring().antMatchers("/css/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .formLogin().loginPage("http://localhost:8080/authentication/require").loginProcessingUrl(FromLoginConstant.LOGIN_PROCESSING_URL)
                .and()
                .authorizeRequests()
                .antMatchers(
                        FromLoginConstant.LOGIN_PROCESSING_URL,
                        FromLoginConstant.LOGIN_PAGE,
                        securityProperties.getOauthLogin().getOauthLogin(),
                        securityProperties.getOauthLogin().getOauthGrant(),
                        "/token/**",
                        "/actuator/**",
                        "/mobile/**").permitAll()
                .antMatchers(HttpMethod.OPTIONS).permitAll()
                .anyRequest().authenticated()
                .and().csrf().disable();
    }
}
